<?php

class Users_ProfileController extends Zend_Controller_Action {

    public function init() {
        
    }

    public function indexAction() {
        $this->checkLoginAction();
        $validation = new Validations_User();
        $db = new Users_Model_Users();
        $user1 = $db->getUser($validation->getUserId());
        $user = $user1[0];
        if ($user['name'] != null) {
            $this->view->name = $user['name'];
            $djson = json_decode($user['contact_number']);
            $contact1 = $djson->contact->primary_contact;
            $contact2 = $djson->contact->secondary_contact;
            $this->view->contact1 = $contact1;
            $this->view->contact2 = $contact2;
            $this->view->address = $user['address'];
            $this->view->email = $user['email'];
        } else {
            $this->view->profileNotSet = true;
            $this->_redirect('/users/profile/update');
        }
    }

    public function updateAction() {
        $this->checkLoginAction();
        $form = new Users_Form_Userprofileupdate();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                print_r($values);
                $db = new Users_Model_Users();
                $validation = new Validations_User();
                $name = $values['name'];
                $address = $values['address'];
                $contact = json_encode(array('contact' => array('primary_contact' => $values['primary_contact'], 'secondary_contact' => $values['sec_contact'])));
                $data = array(
                    'name' => $name,
                    'contact_number' => $contact,
                    'address' => $address
                );
                $db->updateUser($data, $validation->getUserId());
                $this->_redirect('/users/profile');
            }
        }
    }

    public function passwordAction() {
        $this->checkLoginAction();
        $validation = new Validations_User();
        $formErrors = array();
        if (!$validation->isFacebook()) {
            $form = new Users_Form_Userpassword();
            $this->view->form = $form;
            if ($this->getRequest()->isPost()) {
                if ($form->isValid($this->getRequest()->getPost())) {
                    $values = $form->getValues();
                    $password = $validation->getInstanceData()->password;
                    $old = $values['old'];
                    $new1 = $values['new1'];
                    $new2 = $values['new2'];

                    if ($password != md5($old)) {// check for old password
                        array_push($formErrors, 'Old password not matched.');
                    } else {
                        if ($new1 != $new2) {
                            array_push($formErrors, 'New Passwords are not matching.');
                        } else {
                            $db = new Users_Model_Users();
                            $data = array('password' => md5($new1));
                            $db->updateUser($data, $validation->getUserId());
                            Zend_Auth::getInstance()->clearIdentity();
                            $this->_redirect('/users/password/success');
                        }
                    }
                }
            }
        } else {
            array_push($formErrors, "You are logged in using Facebook. You can not change password from here.");
        }
        if (sizeof($formErrors) > 0) {
            $this->view->formErrors = $formErrors;
        }
    }

    public function successAction() {
        
    }

    public function checkLoginAction() {
        $userValidation = new Validations_User();
        $valid = $userValidation->hasidentity();
        if ($valid && $userValidation->getUserTypeId() == 2) {
            
        } else {
            $this->_redirect('/users/login');
        }
    }

}

